package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //用户添加购物车 口味 套餐 菜品
        //获取当前用户的id 查询用户的id购物车下面是否有该套餐 有则+1
        Long id = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        //获取当前请求用户的userid
        shoppingCart.setUserId(id);
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        //如果当前套餐菜品已经有了则+1
        if (shoppingCartList.size() > 0&& shoppingCartList.size() == 1) {
            shoppingCart = shoppingCartList.get(0);
            //数量+1
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //说明该套餐/菜品第一次加入购物车 判断套餐/菜品是否都有
            //菜品不为空
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId!=null){
                Dish byId = dishMapper.getById(dishId);
                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setName(byId.getName());
            }else {
                //套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal byId = setmealMapper.getById(setmealId);

                shoppingCart.setImage(byId.getImage());
                shoppingCart.setAmount(byId.getPrice());
                shoppingCart.setName(byId.getName());


            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }

    }
    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    /**
     * 清空购物车商品
     * @return
     */
    public void cleanShoppingCart() {
        shoppingCartMapper.deleteById(BaseContext.getCurrentId());
    }
}
